//
//  DeviceShareInPhoneVC.m
//  KEREN
//
//  Created by MW on 2023/3/27.
//

#import "DeviceShareInPhoneVC.h"
#import "DeviceShareInviteVC.h"
#import "CJTextField.h"
@interface DeviceShareInPhoneVC ()<UITextFieldDelegate>
@property (nonatomic, strong) NSMutableArray *textfieldArrM;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, copy) NSString *photoStr;
@end

@implementation DeviceShareInPhoneVC

- (void)viewDidLoad {
    [super viewDidLoad];
    //TODO :国际化
    [self setNavViewWithRightTitle:@"确定" navColorIswhite:YES withTitle:@"添加分享账号"];
    _photoStr = @"";
    WEAKSELF
    self.rightClickBlock = ^(void){
        DeviceShareInviteVC *vc = [[DeviceShareInviteVC alloc] init];
        vc.deviceInfo = weakSelf.deviceInfo;
        [weakSelf setPhotoStr];
        vc.photoStr = weakSelf.photoStr;
        if(vc.photoStr.length < 11){
            return;
        }
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    [self setViewLayout];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(deleteTextFlied) name:@"deleteTextFlied" object:nil];

}

- (void)setPhotoStr{
    _photoStr = @"";
    for (int i = 0; i < _textfieldArrM.count; i++) {
        UITextField *textFieldxt = _textfieldArrM[i];
        _photoStr = [_photoStr stringByAppendingString:textFieldxt.text];
    }
    
}

- (void)setViewLayout{
    WEAKSELF
    UILabel *tipsLbl = [UILabel setLblText:@"请输入对方用于注册账号的手机号" font:Font(14) withColor:Color_86898E inSubView:self.view];
    [tipsLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(78 + STATUSBAR_HEIGHT);
        make.left.offset(24);
        make.right.offset(-24);
    }];
    float numbWidth = (DEVICE_WIDTH - 48 - 40) / 11;
    for (int i = 0; i < 11; i ++) {
        CJTextField *textfield = [[CJTextField alloc] init];
        textfield.keyboardType = UIKeyboardTypeNumberPad;
        if(i == 0){
            [textfield becomeFirstResponder];
            textfield.enabled = YES;
        }else{
            textfield.enabled = NO;
        }
        textfield.textColor = Color_333333;
        textfield.font = Font(36);
        [self.view addSubview:textfield];
        [textfield mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(24 + i * (numbWidth + 4));
            make.top.equalTo(tipsLbl.mas_bottom).offset(14);
            make.height.offset(50);
            make.width.offset(numbWidth);
        }];
        textfield.delegate = self;
        textfield.backgroundColor = Whire_Color;
        textfield.layer.cornerRadius = 4;
        textfield.clipsToBounds = YES;
        textfield.tintColor = [UIColor clearColor];
        textfield.textAlignment = NSTextAlignmentCenter;
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:)
                                                    name:@"UITextFieldTextDidChangeNotification"
                                                  object:textfield];
        textfield.cj_delegate = weakSelf;
        [self.textfieldArrM addObject:textfield];
        
    }
    _lineView = [[UIView alloc] init];
    [self.view addSubview:_lineView];
    _lineView.backgroundColor = Color_7ACC00;
    [self updateLineViewFrame];
    
    UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:clearBtn];
    [clearBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-14);
        make.width.height.offset(40);
        make.centerY.equalTo(tipsLbl);
    }];
    [clearBtn setImage:[UIImage imageNamed:@"clear"] forState:UIControlStateNormal];
    [clearBtn addTarget:self action:@selector(clearPhoneNub) forControlEvents:UIControlEventTouchUpInside];
}

- (void)deleteTextFlied
{
    NSArray *textArr = [[_textfieldArrM reverseObjectEnumerator] allObjects];
    for (UITextField *textField in textArr) {
        if(textField.text.length > 0){
            textField.text = @"";
        }
    }
    [self updateLineViewFrame];
}


-(void)textFiledEditChanged:(NSNotification*)notification{
    UITextField *textField = notification.object;
    NSString *str = textField.text;
    if(str.length > 1){
        str =  [str substringToIndex:1];
        textField.enabled = NO;
    }
    textField.text = str;
    [self updateLineViewFrame];
}

- (void)updateLineViewFrame{
    for (UITextField *textField in _textfieldArrM) {
        if(textField.text.length == 0){
            [_lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(textField.mas_bottom).offset(-6);
                make.height.offset(2);
                make.left.equalTo(textField.mas_left).offset(6);
                make.right.equalTo(textField.mas_right).offset(-6);
            }];
            textField.enabled = YES;
            [textField becomeFirstResponder];
            break;
        }
    }
}

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    [self clearPhoneNub];
    return YES;
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    
}


- (void)clearPhoneNub{
    NSArray *textArr = [[_textfieldArrM reverseObjectEnumerator] allObjects];
    for (UITextField *textField in textArr) {
        if(textField.text.length > 0){
            textField.text = @"";
            break;
        }
    }
    [self updateLineViewFrame];
}

-(NSMutableArray *)textfieldArrM{
    if(!_textfieldArrM){
        _textfieldArrM = [NSMutableArray array];
    }
    return _textfieldArrM;
}
@end
